package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.to.SkuHasStockVO;
import com.atguigu.common.to.SkuInfoTO;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //skuId:
        //wareId:
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = params.get("skuId") != null ? (String) params.get("skuId") : "";
        if (StringUtils.isNotBlank(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }

        String wareId = params.get("wareId") != null ? (String) params.get("wareId") : "";
        if (StringUtils.isNotBlank(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1、判断如果还没有这个库存记录：新增
        int count = this.count(
                new QueryWrapper<WareSkuEntity>()
                        .eq("sku_id", skuId)
                        .eq("ware_id", wareId));
        if (count > 0) {
            this.baseMapper.addStock(skuId, wareId, skuNum);
        } else {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            try {
                //远程查询sku名字，如果失败，整个事务无需回滚
                //1、自己catch异常
                //2、TODO 还可以用什么办法，让异常出现以后不会滚？高级部分
                R r = productFeignService.info(skuId);
                if (r.getCode() == 0) {
//                    SkuInfoTO skuInfo = (SkuInfoTO) r.get("skuInfo");
                    Map<String, Object> skuInfo = (Map<String, Object>) r.get("skuInfo");
                    wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
                }
            } catch (Exception e) {

            }
            this.save(wareSkuEntity);
        }
    }

    @Override
    public List<SkuHasStockVO> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVO> stockVOS = new ArrayList<>();
        stockVOS = skuIds.stream().map(skuId -> {
            SkuHasStockVO stockVO = new SkuHasStockVO();
            //查询当前sku的总库存量
            //select sum(stock-stock_locked) from wms_ware_sku where sku_id = 11
            Long count = this.baseMapper.getSkuStock(skuId);
            count = (count == null) ? 0 : count;
            stockVO.setSkuId(skuId);
            stockVO.setHasStock(count > 0);
            return stockVO;
        }).collect(Collectors.toList());
        return stockVOS;
    }

}